package LeetCode;

/**
 * @author VX5
 * @Title: MJC
 * @ProjectName DataStructure
 * @Description: TODO
 * @date ${DAT}22:16
 */
public class LeetCode69 {
    public int mySqrt(int x) {
        if (x == 0){
            return 0;
        }
        int ans = (int) Math.exp(0.5*Math.log(x));
        return ((long)(ans + 1) * (ans + 1) <= x ? ans + 1 : ans);
    }

    public int mySqr2(int x) {
        if (x == 0){
            return 0;
        }
        int l = 0;
        int r = x;
        int re = 0;
        while (l <= r) {
            int mid = (l + r) / 2;
            if ((long)mid*mid > x){
                r = mid;
            }else if ((long)mid*mid < x){
                l = mid + 1;
            }else {
                re = mid;
                break;
            }
        }
        return re;
    }
}
